from django.db import models
from apps.oaauth.models import OAUser,OADepartment

#通知的模型
class Inform(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)
    #如果前端上传的departments中包含了0,比如【0】，那就任务该通知所有部门可见
    public = models.BooleanField(default=False)
    author = models.ForeignKey(OAUser, on_delete=models.CASCADE, related_name='informs',related_query_name='informs')
    #可看见通知的部门:多对多关系
    departments = models.ManyToManyField(OADepartment,related_name='informs',related_query_name='informs')
    #departments：序列化的时候用，前端上传部门id,通过department_ids来获取
    class Meta():
        ordering = ('-create_time',) #按创建时间降序

#用户查看了通知的模型
class InformRead(models.Model):
    inform = models.ForeignKey(Inform,on_delete=models.CASCADE,related_name='reads',related_query_name='reads')
    user = models.ForeignKey(OAUser,on_delete=models.CASCADE,related_name="reads",related_query_name='reads')
    read_time = models.DateTimeField(auto_now_add=True)
    class Meta():
        #指定某些字段组合必须是唯一的
        unique_together = ('inform','user')  #每个通知和每个用户之间的查看关系是唯一的